package com.xinyu.cli.example;

import picocli.CommandLine;

import java.util.concurrent.Callable;

/**
 * 交互式输入
 *
 * @author: Wuxinyu
 * @date: 2024/5/19-05-19-15:11
 * @description: com.xinyu.cli.example
 * @version: 1.0
 */
public class Login2 implements Callable<Integer> {
    @CommandLine.Option(names = {"-u", "--user"}, description = "User name")
    String user;

    @CommandLine.Option(names = {"-p", "--password"}, arity = "0..1", description = "Passphrase", interactive = true)
    String password;

    @CommandLine.Option(names = {"-cp", "--checkPassword"}, description = "Check Password", interactive = true)
    String checkPassword;

    public Integer call() throws Exception {
        // 打印出密码
        System.out.println("密码是：" + password);
        System.out.println("确认密码是：" + checkPassword);
        return 0;
    }

    public static void main(String[] args) {
        new CommandLine(new Login()).execute("-u", "user123", "-p");
        //new CommandLine(new Login()).execute("-u", "user123", "-p", "-cp");

        // new CommandLine(new Login()).execute("-u", "user123", "-p", "xxx","-cp");
        //解决办法1：arity = "0..1" 可选交互式
        //解决办法2：OptionUtil 强制交互式
//        String[] dealArgs = new String[]{"-u", "user123", "-p"};
//        new CommandLine(new Login()).execute(OptionUtil.processInteractiveOptions(Login.class, dealArgs));
    }
}
